function formatNumber(str) {
  if (str == null) {
    return 0;
  }
  if (str === 0 || str === '0') {
    return '0';
  } else if (str == undefined || str == '') {
    return '--';
  }
  let n = str + '';
  let re = /\d{1,3}(?=(\d{3})+$)/g;
  let n1 = n.replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) {
    return s1.replace(re, "$&,") + s2;
  });
  return n1;
}

/**
 * 格式价格
 *
 * @param {String} price
 * @returns 格式化后的价格123,123.00
 */
function formatPrice(price) {
  if (price === 0 || price === '0') {
    return '0.00';
  } else if (price == null || price == undefined || price === "" || isNaN(parseFloat(price))) {
    return "0.00";
  } else {
    price = parseFloat(price).toFixed(2);
    var re = "/\d{1,3}(?=(\d{3})+$)/g";
    price = price + '';
    var n1 = price.replace("/^(\d+)((\.\d+)?)$/", function (s, s1, s2) {
      return s1.replace(re, "$&,") + s2;
    });
    return n1;
  }

}

function shortTime(str) {
  if (str) {
    str = str.substring(5)
  }

  return str || '';
}
// 获取时间格式
function getDateFormat(str) {
  if (str) {
    str = str.replace(/-/g, "/")
    var date = new Date(str);
    //获取年  
    var Y = date.getFullYear();
    //获取月  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    console.log("当前日期：" + Y + '年' + M + '月' + D + '日' + hour);

    return [M, D, hour, minute, s]
  } else {
    return ['', '', '', '', '']
  }

}

// 时间比较
function compareDate(startTime, endTime, currentTime) {
  var start_date = null;
  var end_date = null;

  if (startTime == null) {
    start_date = new Date();

  } else {
    start_date = new Date(startTime.replace(/-/g, "/"));

  }


  if (endTime == null) {
    end_date = new Date();
  } else {
    end_date = new Date(endTime.replace(/-/g, "/"));

  }
  // 6.27   - 5.28
  var fl = end_date > start_date ? true : false;
  return fl;
}
//活动状态
function ActivityStaut(data, currentTime) {


  if (!data) {
    return;
  }

  var state = data.state;
  if (state && (state == 'STOP' || state == 'CANCEL' || state == 'OVER')) { //活动状态

    if (state == 'STOP') {
      staut == 100;
    } else if (state == 'CANCEL') {
      staut == 101;
    } else {
      staut = 3;
    }

    data.activityStaut = staut

    return data;
  }



  var startTime = data.startTime;

  // var endTime = '2022-01-22 10:14:00';
  var endTime = data.endTime;
  var curTime = currentTime ? currentTime : data.curTime;
  var start = compareDate(curTime, startTime); // true 为未开始

  var end = compareDate(endTime, curTime); // true 为结束 
  var staut = 0;
  console.log('活动开始时间' + startTime + '活动当前时间' + curTime)
  if (start) {
    staut = 0; //未开始
    console.log('活动未开始')

    data.titleDates = getDateFormat(startTime)


  } else {
    if (!end) {

      staut = 1; //进行中
      console.log("进行中开始：" + startTime + '结束:' + endTime)
      endTime = endTime.replace(/-/g, "/")
      var msTime = +new Date(endTime)
      //   if(!data.timer){
      //     data.timer = setInterval(countDown, 1000, msTime);
      //   }


      data.titleDates = getDateFormat(endTime)

    } else {

      console.log("已结束开始：" + startTime + '结束:' + endTime)

      staut = 2; //已结束
      var publishLotteryTime = data.publishLotteryTime;
      publishLotteryTime = publishLotteryTime.replace(/-/g, "/")

      var publicFlag = compareDate(publishLotteryTime, curTime); // true 已结束但是 未到中签时间

      var endFlag = compareDate(endTime, publishLotteryTime); // true 已结束但是 未到中签时间

      if (publicFlag) {
        console.log("抽奖：" + startTime + '结束:' + endTime)
        data.titleDates = getDateFormat(publishLotteryTime)

        staut = 3;
      } else {
        if (endFlag) {

          data.titleDates = getDateFormat(publishLotteryTime)

          staut = 2;
        } else { //已结束 到了中签时间

          data.titleDates = getDateFormat(publishLotteryTime)

          staut = 3;
        }
      }

    }
  }

  data.activityStaut = staut

  return data;
  console.log(staut)
}

function formatDate(value) {
  //不能使用 new Date()

  var time = value;
  var year = time.getFullYear();
  var month = time.getMonth() || 1;
  var date = time.getDate();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  month = month < 10 ? "0" + month : month;
  date = date < 10 ? "0" + date : date;
  hour = hour < 10 ? "0" + hour : hour;
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;
  return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

function formatDateReal(value) {
  //不能使用 new Date()

  var time = value;
  var year = time.getFullYear();
  var month = time.getMonth() + 1;
  var date = time.getDate();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  month = month < 10 ? "0" + month : month;
  date = date < 10 ? "0" + date : date;
  hour = hour < 10 ? "0" + hour : hour;
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;
  return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

// 数组更加数据字段分组
function groupByList(datalist, name) {
  let resList = []; //分组结果
  for (var i = 0; i < datalist.length; i++) {
    var thisindex = parseInt(datalist[i][name]); //分组的索引 
    if (resList[thisindex] != undefined) { //如果数组中有值，则添加第二条数据
      resList[thisindex] = resList[thisindex].concat(datalist[i]);
    } else { //新建数组
      resList[thisindex] = [];
      resList[thisindex][0] = datalist[i];
    }
  }

  var list = []

  for (var i = 0; i < resList.length; i++) {
    if (resList && resList[i]) {
      var item = resList[i][0][name]
      console.log('!!!!!', item)
      list.push({
        keyName: item,
        list: resList[i]
      })
    }

  }

  return list
}



module.exports = {
  formatNumber,
  ActivityStaut,
  formatPrice,
  formatDate,
  formatDateReal,
  groupByList
};